﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using DevExpress.XtraGrid.Views.Grid;
using DevExpress.Data.Filtering;
using DevExpress.XtraGrid.Views.Base;
using DevExpress.XtraEditors.Controls;
using System.Collections;
using DevExpress.XtraGrid.Views.Grid.ViewInfo;
using DevExpress.Utils.Drawing;
using DevExpress.XtraGrid.Columns;
using DevExpress.XtraGrid;

namespace QuanLyThuNgan
{
    public partial class frm_NhapChiPhiDichVuNoiTru: DevExpress.XtraEditors.XtraForm
    {
        public frm_NhapChiPhiDichVuNoiTru()
        {
            InitializeComponent();
            init();
        }
        DonVi_Khac dv_khac = new DonVi_Khac();
        BenhNhanInfo bnhanInfo = new BenhNhanInfo();
        FeentCTL fntctl = new FeentCTL();
        FeentDTO fntinfo = new FeentDTO();
        FeentMstDTO fntmstinfo = new FeentMstDTO();
        FeentMstCTL fntmstctl = new FeentMstCTL();
        DataRowView drTong; decimal tongchiphi=0 ;
        DataTable dtdmdvu;
        //public  static bool in
        bool flag_TapPhieu = false; string tenguoixacnhan="";
        private void frm_DanhSachBenhNhan_Load(object sender, EventArgs e)
        {
            //this.Focus();
            Load_TapNhapTrucTiep();
            Load_TapDanhSachBenhNhan(); 
            NguoiDungCTL ndctl = new NguoiDungCTL();
            DataTable dtnd = ndctl.TrangThaiCaThuNgan_Nhap_XacNhan(frmLogin.nd.IDNguoiDung);
            if (dtnd.Rows.Count == 0 || dtnd.Rows[0]["loaiid"].ToString() == "0")
            {
                btnLuu_Nhap.Visible = false;
                btnIn_Nhap.Visible = false;
                btnIn_Phieu.Visible = false;
                btnInPhieuDC_DSPhieu.Visible = false;
                btnInTongHop_DSPhieu.Visible = false;
                btnCapNhatPhieu_DSPhieu.Visible = false;
                btn_CapNhat_Nhap.Visible = false;
                XtraMessageBox.Show("Bạn chỉ có quyền xem");
            }
            else
            {
                tendonvithutien = dtnd.Rows[0]["tendonvi"].ToString();
                string dangottnao = dtnd.Rows[0]["loai"].ToString();//la id dang co  nhiem vu gi?
                string tencalamviec = dtnd.Rows[0]["tencalamviec"].ToString();
                tenguoixacnhan = dtnd.Rows[0]["HoTen"].ToString();
            }
           

        }
        ////////////////////////////
        #region "Tap Danh Sach Phieu"
        private void Load_TapDanhSachPhieu()
        {
            txtSoHoSo_DSPhieu.Text = bnhanInfo.MaSo;
            txtHoTen_DSPhieu.Text =  bnhanInfo.Ten;
            txtTuoi_DSPhieu.Text = bnhanInfo.Tuoi.ToString();
            txtGioiTinh_DSPhieu.Text = bnhanInfo.Gioi;
            txtDiaChi_DSPhieu.Text = bnhanInfo.DiaChi;
            string ngay = Uit.it_Date.CurrentDate("yyyy-MM-dd");
            DataTable dt = dv_khac.LoadDSPhieu_NoiTru(txtSoHoSo_DSPhieu.Text, ngay);
            gridControl_DSPhieu.DataSource = dt;
        }
        private void gridView_DanhSachBN_KeyUp(object sender, KeyEventArgs e)
        {
            if (e.KeyData == Keys.Enter)
            {
                DataRow dr = gridView_DanhSachBN.GetFocusedDataRow();
                if (dr == null) return;
                bnhanInfo.MaSo = dr["SoHS"].ToString();
                bnhanInfo.Ho = dr["Ho"].ToString();
                bnhanInfo.Ten = dr["Ho"].ToString()+" "+dr["Ten"].ToString();
                bnhanInfo.Gioi = dr["GioiTinh"].ToString();
                bnhanInfo.NamSinh = Uit.it_Parse.ToInteger(dr["NamSinh"]);
                bnhanInfo.Tuoi = Uit.it_Parse.ToInteger(dr["Tuoi"]);
                bnhanInfo.DiaChi = dr["DiaChi"].ToString();
                bnhanInfo.BHYTSet = Uit.it_Parse.ToBoolean(dr["bhytset"]);
                bnhanInfo.Ngaybhyt = dr["ngaybhyt"].ToString();
                bnhanInfo.Tendonvi = dr["tendonvi"].ToString();
                bnhanInfo.Madonvi = dr["madonvi"].ToString();
                //txtHo_DS.Text = bnhanInfo.Ho;
                //txtTen_DS.Text = bnhanInfo.Ten;
                //txtNamSinh_DS.Text = bnhanInfo.NamSinh.ToString();
                //txtSoHS_CDCLS.Text = bnhanInfo.MaSo;
                txtHoTen_Nhap.Text =  bnhanInfo.Ten;
                txtSoHS_Nhap.Text = bnhanInfo.MaSo;
                txtTuoi_Nhap.Text = bnhanInfo.Tuoi.ToString();
                txtGioitinh_Nhap.Text = bnhanInfo.Gioi;
                OpenTap(xtraTabPage_Nhap.Name);
            }
        }

        private void gridView_DSPhieu_KeyUp(object sender, KeyEventArgs e)
        {
            if (e.KeyData == Keys.Enter)
            {
                DataRow dr = gridView_DSPhieu.GetDataRow(gridView_DSPhieu.FocusedRowHandle);
                if (dr != null)
                {
                    flag_TapPhieu = true;
                    fntmstinfo.Ssobn = dr["sobn"].ToString();
                    fntmstinfo.IIDLoaiPhieu = int.Parse(dr["IDLoaiPhieu"].ToString());
                    fntmstinfo.Tenloaiphieu = dr["TenLoaiDichVu"].ToString();
                    fntmstinfo.Iidbacsi = int.Parse(dr["idbacsi"].ToString());
                    fntmstinfo.Smadvpk = dr["madvpk"].ToString();
                    fntmstinfo.SMaPhieuChiDinh = dr["MaPhieuChiDinh"].ToString();
                    fntmstinfo.SGhiChuUni = dr["GhiChuUni"].ToString();
                    fntmstinfo.SChanDoanUni = dr["ChanDoanUni"].ToString();
                    fntmstinfo.IIDNguoiNhap = int.Parse(dr["IDNguoiNhap"].ToString());
                    OpenTap(xtraTabPage_Nhap.Name);
                    ExpandAllRows(gridView_DMDV, fntmstinfo.IIDLoaiPhieu - 1, true);
                }
            }
        }

        private void btnLuu_Phieu_Click(object sender, EventArgs e)
        {
            XtraMessageBox.Show("Lưu Thành Công");
        }

        private void btnIn_Phieu_Click(object sender, EventArgs e)
        {
            XtraMessageBox.Show("In Thành Công");
        }

        #endregion
        ///////////////////////////// 
        #region "Tap Danh Sach Benh Nhan"
        private void Load_TapDanhSachBenhNhan()
        {
            DataTable dtkt = new DataTable();
            CaThuNganCTL ctl = new CaThuNganCTL();
            dtkt = ctl.m_SelectKiemTra(frmLogin.nd.IDNguoiDung);
            DataTable dtDonVi = dv_khac.DMDonVi();
            ClassHelp.m_SearchLookUpEdit(dtDonVi, searchLookUpEditKhoa, "TenDonVi", "MaDonVi");
            ResetTapBenhNhan();
        }
        private void btnCancel_Click(object sender, EventArgs e)
        {
           ResetTapBenhNhan();
             //gridControl_DSBenhNhan.sho
        }
        void ResetTapBenhNhan()
        {
            dtTuNgay.EditValue = Uit.it_Date.CurrentDate2();
            dtDenNgay.EditValue = Uit.it_Date.CurrentDate2();
            bnhanInfo = new BenhNhanInfo();
            txtHoTen_DS.Text = "";
            txtTen.Text = "";
            txtNamSinh_DS.Text = "";
            txtSoHoSo_DS.Text = "";
            txtSoNhapVien_DS.Text = "";
            gridControl_DSBenhNhan.DataSource =DBNull.Value ;
        }
        private void btnTimKiem_DS_Click(object sender, EventArgs e)
        {
            DataTable dtdsbenhnhan = dv_khac.TimKiemDSBNNoiTru(txtSoHoSo_DS.Text, txtHoTen_DS.Text, txtTen.Text,
            txtNamSinh_DS.Text == "" ? 0 : Uit.it_Parse.ToInteger(txtNamSinh_DS.Text), txtSoNhapVien_DS.Text,
            searchLookUpEditKhoa.Text == "" ? "" : Uit.it_Parse.ToString(searchLookUpEditKhoa.EditValue), Uit.it_Parse.ToDateTime(dtTuNgay.EditValue).ToString("yyyy-MM-dd"), Uit.it_Parse.ToDateTime(dtDenNgay.EditValue).ToString("yyyy-MM-dd"));
            gridControl_DSBenhNhan.DataSource = dtdsbenhnhan;
        }
        private void gridView_DanhSachBN_DoubleClick(object sender, EventArgs e)
        {
            DataRow dr = gridView_DanhSachBN.GetDataRow(gridView_DanhSachBN.FocusedRowHandle);
            if (dr == null) return;
            bnhanInfo.MaSo = dr["SoHS"].ToString();
            bnhanInfo.Ho = dr["Ho"].ToString();
            bnhanInfo.Ten = dr["Ho"].ToString() + " " + dr["Ten"].ToString();
            bnhanInfo.Gioi = dr["GioiTinh"].ToString();
            bnhanInfo.NamSinh = Uit.it_Parse.ToInteger(dr["NamSinh"]);
            bnhanInfo.Tuoi = Uit.it_Parse.ToInteger(dr["Tuoi"]);
            bnhanInfo.DiaChi = dr["DiaChi"].ToString();
            bnhanInfo.BHYTSet = Uit.it_Parse.ToBoolean(dr["bhytset"]);
            bnhanInfo.Ngaybhyt = dr["ngaybhyt"].ToString();
            bnhanInfo.Tendonvi = dr["tendonvi"].ToString();
            bnhanInfo.Madonvi = dr["madonvi"].ToString();
            txtHoTen_DS.Text = bnhanInfo.Ho;
            txtTen.Text = bnhanInfo.Ten;
            txtNamSinh_DS.Text = bnhanInfo.NamSinh.ToString();
            txtSoHoSo_DS.Text = bnhanInfo.MaSo;
            txtHoTen_Nhap.Text = bnhanInfo.Ten;
            txtSoHS_Nhap.Text = bnhanInfo.MaSo;
            txtTuoi_Nhap.Text = bnhanInfo.Tuoi.ToString();
            txtGioitinh_Nhap.Text = bnhanInfo.Gioi;
            bnhanInfo.SoNhapVien = dr["sonhapvien"].ToString();
            txtSoNhapVien_DS.Text = dr["sonhapvien"].ToString();
            txtSoNhapVien_NL.Text = dr["sonhapvien"].ToString();
            OpenTap(xtraTabPage_Nhap.Name);
        }
        private void gridView_DanhSachBN_FocusedRowChanged(object sender, FocusedRowChangedEventArgs e)
        {
            DataRow dr = gridView_DanhSachBN.GetDataRow(e.FocusedRowHandle);
            if (dr == null) return;
            bnhanInfo.MaSo = dr["SoHS"].ToString();
            bnhanInfo.Ho= dr["Ho"].ToString();
            bnhanInfo.Ten = dr["Ho"].ToString() + " " + dr["Ten"].ToString();
            bnhanInfo.Gioi = dr["GioiTinh"].ToString();
            bnhanInfo.NamSinh =Uit.it_Parse.ToInteger( dr["NamSinh"]);
            bnhanInfo.Tuoi = Uit.it_Parse.ToInteger(dr["Tuoi"]);
            bnhanInfo.DiaChi =dr["DiaChi"].ToString();
            bnhanInfo.BHYTSet = Uit.it_Parse.ToBoolean(dr["bhytset"]);
            bnhanInfo.Ngaybhyt = dr["ngaybhyt"].ToString();
            bnhanInfo.Tendonvi = dr["tendonvi"].ToString();
            bnhanInfo.Madonvi = dr["madonvi"].ToString();
            txtHoTen_DS.Text = bnhanInfo.Ho;
            txtTen.Text = bnhanInfo.Ten;
            txtNamSinh_DS.Text = bnhanInfo.NamSinh.ToString();
            txtSoHoSo_DS.Text = bnhanInfo.MaSo;
            txtHoTen_Nhap.Text =  bnhanInfo.Ten;
            txtSoHS_Nhap.Text = bnhanInfo.MaSo;
            txtTuoi_Nhap.Text = bnhanInfo.Tuoi.ToString();
            txtGioitinh_Nhap.Text = bnhanInfo.Gioi;
            txtSoNhapVien_DS.Text = dr["sonhapvien"].ToString();
            txtSoNhapVien_NL.Text = dr["sonhapvien"].ToString();
        }
        #endregion
        /////////////////////////////
        #region "Tap Nhap Lieu"
        string _sqlbf = "", _sqlbfmst = "", _sqlbfdelete = "";
        private int idbacsi_nhap = -1; string madvpk_nhap = " ";
        private bool isChangeData = false, isChangeData1 = false;
        private int rowtext, leveltext;
        private ArrayList arrsbn = new ArrayList();
        private int rowdetail; int sodong = 0; object machung = null, soluong;
        GridView detailView; string tendonvithutien = "";
        public   dsThuNgan ds;
        DataTable dtload_dvyt = ThuNgan.st_loadctdvcap1(4);
        DataTable dtload_vtyt = ThuNgan.st_loadctdvcap1(3);
        DataTable dtload_ct_cls = ThuNgan.st_loadctdvcap2_cls_all();
        DataView dtload_ct_cls_view, dtload_child_ptgp_view;
        DataTable dtload_child_ptgp = ThuNgan.st_loadctdvcap2_ptgp_all();
        string madv_cls = "";// khi chon cls nao thi lay ma cls do -> de load gridlookEdit theo ma dich vu cls
        string maso_ptgp = "";// dung de lay ma de  load du lieu repositoryGridLoookEdit_ct_ptgp
        void init()
        {
            gridControl_NhapLieu.FocusedViewChanged += new DevExpress.XtraGrid.ViewFocusEventHandler(gridControl_NhapLieu_FocusedViewChanged);
            //  gridControl_NhapLieu.ProcessGridKey+=new KeyEventHandler(gridControl_NhapLieu_ProcessGridKey);
            gridView_CLS.FocusedRowChanged += new FocusedRowChangedEventHandler(gridView_CLS_FocusedRowChanged);
            gridView_CLS.KeyUp += new KeyEventHandler(gridView_CLS_KeyUp);
            gridView_CLS.MasterRowExpanded += new CustomMasterRowEventHandler(gridView_CLS_MasterRowExpanded);
            gridView_CT_CLS.CellValueChanged += new CellValueChangedEventHandler(gridView_CT_CLS_CellValueChanged);
            gridView_CT_CLS.KeyUp += new KeyEventHandler(gridView_CT_CLS_KeyUp);
            gridView_CT_PTGP.KeyUp += new KeyEventHandler(gridView_CT_PTGP_KeyUp);
            gridView_CT_PTGP.CellValueChanged += new CellValueChangedEventHandler(gridView_CT_PTGP_CellValueChanged);
            gridView_PTGP.FocusedRowChanged += new FocusedRowChangedEventHandler(gridView_PTGP_FocusedRowChanged);
            gridView_PTGP.KeyUp += new KeyEventHandler(gridView_PTGP_KeyUp);
            gridView_PTGP.MasterRowExpanded += new CustomMasterRowEventHandler(gridView_PTGP_MasterRowExpanded);
            gridView_VTYT.KeyUp += new KeyEventHandler(gridView_VTYT_KeyUp);
            gridView_VTYT.CellValueChanged += new CellValueChangedEventHandler(gridView_VTYT_CellValueChanged);
            gridView_DVYT.CellValueChanged += new CellValueChangedEventHandler(gridView_DVYT_CellValueChanged);
            gridView_DVYT.KeyUp +=new KeyEventHandler(gridView_DVYT_KeyUp);
            gridView_DMDV.MasterRowExpanding += new MasterRowCanExpandEventHandler(gridView_DMDV_MasterRowExpanding);
            gridView_DMDV.FocusedRowChanged += new FocusedRowChangedEventHandler(gridView_DMDV_FocusedRowChanged);
            gridView_DMDV.MasterRowExpanded += new CustomMasterRowEventHandler(gridView_DMDV_MasterRowExpanded);
            // gridView_DMDV.CustomDrawCell +=new RowCellCustomDrawEventHandler(gridView_DMDV_CustomDrawCell);
        }
        private void Load_TapNhapTrucTiep()
        {
            // load thong tin benh nhan
            DataTable dtBS = dv_khac.DMBacSi();
            DataTable dtDonVi = dv_khac.DMDonVi();
            ClassHelp.m_SearchLookUpEdit(dtBS, cbosearchBacSi_Nhap, "TenBS", "IDBS");
            ClassHelp.m_SearchLookUpEdit(dtDonVi, cboseachDonVi_Nhap, "TenDonVi", "MaDonVi");
            // load chitiet dich vu
            ds = new dsThuNgan();
            // danh muc dich vu 1.cls 2. pttp... 
            dtdmdvu = ThuNgan.st_loaddichvu();
            for (int k = 0; k < dtdmdvu.Rows.Count - 1;k++)
            {
                //if(dtdmdvu.Rows.Count
                DataRow drset = ds.dt_dmloaiphieu.NewRow();
                drset["IDLoaiPhieu"] = dtdmdvu.Rows[k]["IDLoaiPhieu"];
                drset["TenLoaiPhieu"] = dtdmdvu.Rows[k]["TenLoaiPhieu"];
                ds.dt_dmloaiphieu.Rows.Add(drset);
            }
            ds.AcceptChanges();
            gridControl_NhapLieu.DataSource = ds;
            gridControl_NhapLieu.DataMember = "dt_dmloaiphieu";
            #region loaddlieu
            //chi tiet dich vu : vat tu y te
            ClassHelp.m_RepositoryItenSearchLookUpEdit(dtload_vtyt, repositoryItemGridLookUpEdit_vtyt1, "tenvt","dvucode");
            gcvtyt.ColumnEdit = repositoryItemGridLookUpEdit_vtyt1;
            //chi tiet dich vu : Dich Vu Y te
            gcdvyt.FieldName = "dvucode";
            ClassHelp.m_RepositoryItenSearchLookUpEdit(dtload_dvyt, repositoryItemGridLookUpEdit_dvyt1, "tendvu", "dvucode");
            gcdvyt.ColumnEdit = repositoryItemGridLookUpEdit_dvyt1;
            //can lam sang
            DataTable dtload_cls = ThuNgan.st_loaidichvu_ct_canlamsang_all();
            ClassHelp.m_RepositoryItenSearchLookUpEdit(dtload_cls, repositoryItemGridLookUpEdit_cls1, "tenloai", "loaidvu");
            gc_cls.ColumnEdit = repositoryItemGridLookUpEdit_cls1;
            //chi tiet can lam sang
            dtload_ct_cls_view = dtload_ct_cls.DefaultView;
            ClassHelp.m_RepositoryItenSearchLookUpEdit_View(dtload_ct_cls_view, repositoryItemGridLookUpEdit_ct_cls1, "tendvu", "dvucode");
            gc_ctcls.ColumnEdit = repositoryItemGridLookUpEdit_ct_cls1;
            //chi tiet phau thuat giai phau
            DataTable dtload_ptgp = ThuNgan.st_loadctdvcap1(2);
            ClassHelp.m_RepositoryItenSearchLookUpEdit(dtload_ptgp, repositoryItemGridLookUpEdit_ptgp1, "chidanh", "loai");
            gcptgp.ColumnEdit = repositoryItemGridLookUpEdit_ptgp1;
            //child phau thuat giai phau
            dtload_child_ptgp_view = dtload_child_ptgp.DefaultView;
            ClassHelp.m_RepositoryItenSearchLookUpEdit_View(dtload_child_ptgp_view, repositoryItemGridLookUpEdit_CT_PTGP1, "chidanh", "dvucode");
            gcptgp_ct.ColumnEdit = repositoryItemGridLookUpEdit_CT_PTGP1;
        }
        private void gridControl_NhapLieu_FocusedViewChanged(object sender, DevExpress.XtraGrid.ViewFocusEventArgs e)
        {
            if (((e.View != null)) && ((e.View.ParentView != null)))
            {
                ((DevExpress.XtraGrid.Views.Base.ColumnView)e.View.ParentView).FocusedRowHandle = e.View.SourceRowHandle;
                drTong = e.View.SourceRow as DataRowView;
                //e.View.DetailTabHeaderLocation   
            }
        }
        private void gridView_VTYT_KeyUp(object sender, KeyEventArgs e)
        {
            if (e.KeyData == Keys.Delete)
            {
                GridView griV = sender as GridView;
                DataRowView datarow1 = drTong;
                DataRow dr = griV.GetDataRow(griV.FocusedRowHandle);
                if (dr["sobn"].ToString() != "")
                   _sqlbfdelete = _sqlbfdelete + " " + fntctl.m_Delete_(dr["sobn"].ToString(), dr["dvucode"].ToString());
                griV.DeleteSelectedRows();
            }
        }
        private void gridView_DVYT_KeyUp(object sender, KeyEventArgs e)
        {
            if (e.KeyData == Keys.Delete)
            {
                GridView griV = sender as GridView;
                DataRowView datarow1 = drTong;
                DataRow dr = griV.GetDataRow(griV.FocusedRowHandle);
                if (dr["sobn"].ToString() != "")
                   _sqlbfdelete = _sqlbfdelete + " " + fntctl.m_Delete_(dr["sobn"].ToString(), dr["dvucode"].ToString());
                griV.DeleteSelectedRows();
            }
        }
        private void gridView_PTGP_KeyUp(object sender, KeyEventArgs e)
        {
            if (e.KeyData == Keys.Delete)
            {
                GridView griV = sender as GridView;
                DataRow dr = griV.GetDataRow(griV.FocusedRowHandle);
                if (dr["sobn"].ToString() != "")
                    _sqlbfdelete = _sqlbfdelete + " " + fntmstctl.m_Delete_group(dr["loai"].ToString(), dr["sobn"].ToString());
                griV.DeleteSelectedRows(); 
            }
        }
        private void gridView_CT_CLS_KeyUp(object sender, KeyEventArgs e)
        {
            if (e.KeyData == Keys.Delete)
            {
                GridView griV = sender as GridView;
                DataRowView datarow1 = drTong;
                DataRow dr = griV.GetDataRow(griV.FocusedRowHandle);
                if (dr["sobn"].ToString() != "")
                 _sqlbfdelete = _sqlbfdelete + " " + fntctl.m_Delete_(dr["sobn"].ToString(), dr["dvucode"].ToString());
                
                griV.DeleteSelectedRows();
            }
        }
        private void gridView_CT_PTGP_KeyUp(object sender, KeyEventArgs e)
        {
            if (e.KeyData == Keys.Delete)
            {
                    GridView griV = sender as GridView;
                    DataRow dr = griV.GetDataRow(griV.FocusedRowHandle);
                    if (dr["sobn"].ToString() != "")
                       _sqlbfdelete = _sqlbfdelete + " " + fntctl.m_Delete_(dr["sobn"].ToString(), dr["dvucode"].ToString());
                    griV.DeleteSelectedRows();
             
            }
        }
        private void gridView_CLS_KeyUp(object sender, KeyEventArgs e)
        {
            if (e.KeyData == Keys.Delete)
            {
                GridView griV = sender as GridView;
                DataRow dr = griV.GetDataRow(griV.FocusedRowHandle);
                if (dr["sobn"].ToString()!="")
                   _sqlbfdelete = _sqlbfdelete + " " + fntmstctl.m_Delete_group(dr["loaidvu"].ToString(),dr["sobn"].ToString());
                griV.DeleteSelectedRows(); ;
            }

        }
        private void gridView_DMDV_MasterRowExpanding(object sender, MasterRowCanExpandEventArgs e)
        {
            rowtext = e.RowHandle;
            leveltext = e.RelationIndex;
        }
        private void gridView_CLS_FocusedRowChanged(object sender, DevExpress.XtraGrid.Views.Base.FocusedRowChangedEventArgs e)
        {
            GridView view = sender as GridView;
            if (view == null) return;
            DataRow dr = view.GetDataRow(view.FocusedRowHandle);
            if (dr == null)
                return;
            madv_cls = dr[0].ToString();
        }
        private void gridView_PTGP_FocusedRowChanged(object sender, DevExpress.XtraGrid.Views.Base.FocusedRowChangedEventArgs e)
        {
            sodong = e.FocusedRowHandle;
        }
        private void gridView_PTGP_CellValueChanged(object sender, DevExpress.XtraGrid.Views.Base.CellValueChangedEventArgs e)
        {
            GridView view = sender as GridView;
            gridView_DMDV.SetMasterRowExpandedEx(rowdetail, 0, true);
            if (view == null) return;
            DataRow dr = view.GetDataRow(view.FocusedRowHandle);
            if (dr == null)
                return;
            maso_ptgp = dr[0].ToString();
        }
        private void repositoryItemGridLookUpEdit_CT_PTGP1_QueryPopUp(object sender, CancelEventArgs e)
        {
            if (drTong != null)
                maso_ptgp = drTong[0].ToString();
            dtload_child_ptgp_view.RowFilter = "dvucode like '" + maso_ptgp + "*'";
            //BinaryOperator filter = new BinaryOperator("dvucode", maso_ptgp + "%", BinaryOperatorType.Like);
            //((DerivedLookUpEdit)sender).Properties.View.ActiveFilter.NonColumnFilterCriteria = filter;
            //((DerivedLookUpEdit)sender).Properties.View.OptionsView.ShowFilterPanelMode = ShowFilterPanelMode.Never;
        }
        private void gridView_PTGP_MasterRowExpanded(object sender, CustomMasterRowEventArgs e)
        {
            GridView gv = sender as GridView;
            gv.OptionsDetail.ShowDetailTabs = false;
            if (detailView == null) return;
            detailView.OptionsDetail.ShowDetailTabs = false;
            detailView.Focus();
            detailView.SetFocusedRowModified();// select tren row cua view doa
            repositoryItemGridLookUpEdit_CT_PTGP1.QueryPopUp +=new CancelEventHandler(repositoryItemGridLookUpEdit_CT_PTGP1_QueryPopUp);
        }
        private void gridView_DMDV_FocusedRowChanged(object sender, DevExpress.XtraGrid.Views.Base.FocusedRowChangedEventArgs e)
        {
            rowdetail = e.FocusedRowHandle;
        }
        private void repositoryItemGridLookUpEdit_ct_cls1_QueryPopUp(object sender, CancelEventArgs e)
        {
            string madv_cls1 = "";
            if (drTong != null)
                madv_cls1 = drTong[0].ToString();
            dtload_ct_cls_view.RowFilter = "dvucode like '" + madv_cls1 + "*'";
        }
        private void gridView_CLS_MasterRowExpanded(object sender, CustomMasterRowEventArgs e)
        {
            GridView gv = sender as GridView;
            gv.OptionsDetail.ShowDetailTabs = false;
            if (detailView == null) return;
            detailView.OptionsDetail.ShowDetailTabs = false;
            detailView.Focus();
            detailView.SetFocusedRowModified();// select tren row cua view doa
            //  detailView.SetMasterRowExpandedEx(detailView.FocusedRowHandle, 0, true);
            repositoryItemGridLookUpEdit_ct_cls1.QueryPopUp += new CancelEventHandler(repositoryItemGridLookUpEdit_ct_cls1_QueryPopUp);
        }
        private void gridView_DMDV_MasterRowExpanded(object sender, CustomMasterRowEventArgs e)
        {
            GridView masterView = sender as GridView;
            detailView = masterView.GetDetailView(e.RowHandle, e.RelationIndex) as GridView;
            masterView.OptionsDetail.ShowDetailTabs = false;
            int idlp = fntmstinfo.IIDLoaiPhieu - 1;//gridView_DMDV.FocusedRowHandle
            if (idlp != e.RowHandle && flag_TapPhieu)
                detailView.OptionsBehavior.ReadOnly = true;
            else
                detailView.OptionsBehavior.ReadOnly = false;
            if (detailView == null) return;
            // detailView.Focus();
            detailView.SetFocusedRowModified();// select tren row cua view doa
            // detailView.SetMasterRowExpandedEx(detailView.FocusedRowHandle, 0, true);
            int s = e.RowHandle;
            switch (s)
            {
                case 0:
                    GridView viewcls = (GridView)detailView.GetDetailView(e.RowHandle, e.RelationIndex);
                    gridView_DMDV.SetMasterRowExpandedEx(0, 5, true);//cls
                    break;
                case 1:
                    gridView_DMDV.SetMasterRowExpandedEx(1, 2, true);//ptgp 
                    break;
                case 2:
                    gridView_DMDV.SetMasterRowExpandedEx(2, 4, true);//dvyt
                    break;
                case 3:
                    gridView_DMDV.SetMasterRowExpandedEx(3, 3, true);//vtyt
                    break;
            }
        }
        private void gridView_VTYT_CellValueChanged(object sender, CellValueChangedEventArgs e)
        {
            GridView view = sender as GridView;
            //gridView_DMDV.SetMasterRowExpandedEx(rowdetail, 0, true);
            if (view == null) return;
            DataRow dr = view.GetDataRow(view.FocusedRowHandle);
            if (dr == null)
                return;
            int currentCol = e.Column.VisibleIndex;
            //  if (currentCol == view.Columns["dvucode"].VisibleIndex)
            machung = dr["dvucode"].ToString();
            if (machung != null)
                m_gridView_CellValueChanged(view, dtload_vtyt, machung, "dvt", "dongia", "dvucode", e);
        }
        private void gridView_CT_CLS_CellValueChanged(object sender, CellValueChangedEventArgs e)
        {
            GridView view = sender as GridView;
            //gridView_DMDV.SetMasterRowExpandedEx(rowdetail, 0, true);
            if (view == null) return;
            DataRow dr = view.GetDataRow(view.FocusedRowHandle);
            if (dr == null)
                return;
            int currentCol = e.Column.VisibleIndex;
                machung = dr["dvucode"].ToString();
            if (machung != null)
                m_gridView_CellValueChanged_CLS(view, dtload_ct_cls, machung, "", "fee", "dvucode", e);
           
        }
        public void  m_gridView_CellValueChanged_CLS(GridView _g, DataTable _dt, object mac, string dvt, string dongia, string id_ma, DevExpress.XtraGrid.Views.Base.CellValueChangedEventArgs e)
        {
            int currentRow = e.RowHandle;
            string currentCol = e.Column.FieldName;
            DataRow dr = _g.GetDataRow(_g.FocusedRowHandle);
            if (currentCol.Trim() == "soluong")
            {
                if (dr["soluong"] != null)
                {
                    object cellsoluong = dr["soluong"];
                    double Dongia = double.Parse(dr["dongia"].ToString());
                    double soluong = 0;
                    if (cellsoluong != null) soluong = Convert.ToDouble(cellsoluong);
                    double ThanhTien = soluong * Dongia;
                    dr["thanhtien"] = ThanhTien;
                }
            }
            else if (currentCol.Trim() == id_ma)
            {
                foreach (DataRow dow in _dt.Rows)
                {
                    if (dow[id_ma].ToString().Trim() == mac.ToString().Trim())
                    {
                        dr["dongia"] = dow[dongia];
                        if (dvt != "")
                        {
                            string donvitinh = dv_khac.DVTChuyenDoi(dow[dvt].ToString());
                            donvitinh = Uit.it_Font.VNIToUnicode(donvitinh);
                            dr["dvt"] = donvitinh;
                        }
                        //else
                        //dr["dvt"] = dow[dvt];
                        //dr["vtidao"] = mac.ToString();
                        //dr["guidi"] = dow["guidi"];
                        dr["soluong"] = 1;
                        if (dr["soluong"] != null)
                        {
                            object cellsoluong = dr["soluong"];
                            double Dongia = double.Parse(dr["dongia"].ToString());
                            double soluong = 0;
                            if (cellsoluong != null) soluong = Convert.ToDouble(cellsoluong);
                            double ThanhTien = soluong * Dongia;
                            dr["thanhtien"] = ThanhTien;
                            return;
                        }
                    }
                }
            }
        }
        public  void m_gridView_CellValueChanged(GridView _g, DataTable _dt, object mac, string dvt, string dongia,string id_ma, DevExpress.XtraGrid.Views.Base.CellValueChangedEventArgs e)
        {
            int currentRow = e.RowHandle;
            string currentCol = e.Column.FieldName;
            DataRow dr = _g.GetDataRow(_g.FocusedRowHandle);
            if (currentCol.Trim() == "soluong")
            {
                if (dr["soluong"] != null)
                {
                    object cellsoluong = dr["soluong"];
                    double Dongia = double.Parse(dr["dongia"].ToString());
                    double soluong = 0;
                    if (cellsoluong != null) 
                    soluong = Convert.ToDouble(cellsoluong);
                    double ThanhTien = soluong * Dongia;
                    dr["thanhtien"] = ThanhTien;
                }
            }
            else if (currentCol.Trim() == id_ma)
            {
                foreach (DataRow dow in _dt.Rows)
                {
                    if (dow[id_ma].ToString().Trim() == mac.ToString().Trim())
                    { 
                        dr["dongia"] = dow[dongia];
                        if (dvt != "")
                        {
                            string donvitinh = dv_khac.DVTChuyenDoi(dow[dvt].ToString());
                            donvitinh = Uit.it_Font.VNIToUnicode(donvitinh);
                            dr["dvt"] = donvitinh;
                        }

                        dr["soluong"] = 1;
                        if (dr["soluong"] != null)
                        {
                            object cellsoluong = dr["soluong"];
                            double Dongia = double.Parse(dr["dongia"].ToString());
                            double soluong = 0;
                            if (cellsoluong != null) soluong = Convert.ToDouble(cellsoluong);
                            double ThanhTien = soluong * Dongia;
                            dr["thanhtien"] = ThanhTien;
                            return;
                        }
                    }
                }
            }
        }
        private void gridView_DVYT_CellValueChanged(object sender, CellValueChangedEventArgs e)
        {
            GridView view = sender as GridView;
            //gridView_DMDV.SetMasterRowExpandedEx(rowdetail, 0, true);
            if (view == null) return;
            DataRow dr = view.GetDataRow(view.FocusedRowHandle);
            if (dr == null)
                return;
            int currentCol = e.Column.VisibleIndex;
            //  if (currentCol == view.Columns["dvucode"].VisibleIndex)
            machung = dr["dvucode"].ToString();
            if (machung != null)
                m_gridView_CellValueChanged(view, dtload_dvyt, machung, "dvt", "dongia", "dvucode", e);
        }
        private void gridView_CT_PTGP_CellValueChanged(object sender, CellValueChangedEventArgs e)
        {
            GridView view = sender as GridView;
            //gridView_DMDV.SetMasterRowExpandedEx(rowdetail, 0, true);
            if (view == null) return;
            DataRow dr = view.GetDataRow(view.FocusedRowHandle);
            if (dr == null)
                return;
            int currentCol = e.Column.VisibleIndex;
            //  if (currentCol == view.Columns["dvucode"].VisibleIndex)
            machung = dr["dvucode"].ToString();
            if (machung != null)
                m_gridView_CellValueChanged(view, dtload_child_ptgp, machung, "", "dongia", "dvucode", e);
        }
        private void btnDSPhieu_Nhap_Click(object sender, EventArgs e)
        {
            OpenTap(xtraTabPage_DSPhieu.Name);
         
        }
        private void btnBNKhac_Nhap_Click(object sender, EventArgs e)
        {
            OpenTap(xtraTabPage_DanhSachBN.Name);
        }  
        int stt=0,sttdonvi=0;//dung cho bnhan va 
        int sttdetang = 0;
        private void Insertdetailview(GridView view, int i,DataTable dt,int Istt)
        {
            decimal tongdongia = 0, tongtienguidi = 0; int sttsbn = 0, stt_ = Istt, idloaiphieu = 0;
            string sobnhamtam = ""; bool ktranull = false; sttdetang = 0;
            if (view != null)
            {
                if (dt.Rows[0]["IDCaThuNgan"].ToString() != "")// kiểm tra có đăng ký ca chưa ?
                {
                    DataRow drvew = view.GetDataRow(i);
                    if (drvew == null) idloaiphieu = 0;
                    else
                        idloaiphieu = i + 1;
                    fntmstinfo.IIDLoaiPhieu = i + 1;
                    if (idloaiphieu == 1)//cls
                    {
                        object madvo; string chuoibfmst = ""; int sothutuDH = 0;
                        int ikk = 0; decimal tongDH = 0; bool co = false; string addsobnDH = "";
                        for (int k = 0; k < view.DataRowCount; k++)
                        {
                            ktranull = false;
                            GridView vieww = (GridView)view.GetDetailView(k, 0);
                            if (vieww != null)
                            {
                                DataRow drmstcls = view.GetDataRow(k);
                                string loaidvu = drmstcls["loaidvu"].ToString();
                                madvo = dv_khac.DonViTuLoaidichVu(loaidvu);
                                DataTable dtt = fntmstctl.m_STT_SoBN_IDThuNgan_noitru(fntmstinfo.IIDNguoiNhap, fntmstinfo.Sdangay, madvo.ToString().Trim(), madvpk_nhap.Trim());
                                if (madvo.ToString().Trim() == "DH")
                                {
                                    fntmstinfo.Ssobn = dtt.Rows[0]["sobn"].ToString();
                                    if (ikk < 1)
                                        arrsbn.Add(fntmstinfo.Ssobn);
                                    if (loaidvu.Trim() == "GA")
                                    {
                                        co = true;
                                        stt_ = stt_ + 1;
                                        if (ikk > 0)
                                        {
                                            string[] s = fntmstinfo.Ssobn.Split('-');
                                            int td = Uit.it_Parse.ToInteger(s[1]) + 1;
                                            string tamduoi = Uit.it_String.Right("000" + td.ToString(), 4);
                                            fntmstinfo.Ssobn = s[0].ToString() + "-" + tamduoi;
                                        }
                                        for (int j = 0; j < vieww.DataRowCount; j++)
                                        {
                                            ktranull = true;
                                            DataRow dr = vieww.GetDataRow(j);
                                            fntinfo.Sobn = fntmstinfo.Ssobn;
                                            fntinfo.Loaidvu = dr["loaidvu"].ToString();
                                            fntinfo.Dvucode = dr["dvucode"].ToString();
                                            fntinfo.SDonViTinh = ""; fntinfo.Sdvt = "";
                                            fntinfo.Istt = j + 1;
                                            fntinfo.Fslg = float.Parse(dr["soluong"].ToString());
                                            fntinfo.Bnotrongoi =Uit.it_Parse.ToBoolean(dr["notrongoi"]);
                                            fntinfo.Ddonggia = decimal.Parse(dr["dongia"].ToString());
                                            fntinfo.Dthanhtien = decimal.Parse(dr["thanhtien"].ToString());
                                            tongtienguidi = tongtienguidi + fntinfo.Dthanhtien;
                                            _sqlbf = _sqlbf + " " + fntctl.m_Insert_(fntinfo.Sobn, fntinfo.Loaidvu, fntinfo.Dvucode,
                                             fntinfo.SDonViTinh, fntinfo.Fslg, fntinfo.Dthanhtien, fntinfo.Ddonggia,
                                             fntinfo.Sdvt, fntinfo.Istt, fntinfo.Dgiabhyt, fntinfo.Bnotrongoi).ToString();

                                        }
                                        fntmstinfo.Sonhapvien = txtSoNhapVien_NL.Text;
                                        fntmstinfo.Dsotien = tongtienguidi;
                                        arrsbn.Add(fntmstinfo.Ssobn);
                                        if (ktranull == true)
                                          _sqlbfmst = _sqlbfmst + " " + fntmstctl.m_Insert_(fntmstinfo.Ssobn, fntmstinfo.Sdangay,
                                          fntmstinfo.Ssohs, fntmstinfo.Dsotien, fntmstinfo.SGhiChuUni, fntmstinfo.Iidbacsi, fntmstinfo.Smadvpk,
                                          fntmstinfo.SChanDoanUni, fntmstinfo.IIDLoaiPhieu, fntmstinfo.Sghichu, fntmstinfo.Schandoan, fntmstinfo.IIDNguoiNhap,fntmstinfo.Sonhapvien).ToString();

                                    }
                                    else //khong thuoc loai gui di "GA"
                                    {
                                        // k = 0;
                                        if (ikk < 1)
                                        {
                                            stt_ = stt_ + 1;
                                            sothutuDH = stt_;
                                        }//
                                        if (co && ikk == 0)
                                        {
                                            string[] s = fntmstinfo.Ssobn.Split('-');
                                            int td = Uit.it_Parse.ToInteger(s[1]) + 1;
                                            string tamduoi = Uit.it_String.Right("000" + td.ToString(), 4);
                                            fntmstinfo.Ssobn = s[0].ToString() + "-" + tamduoi;
                                            sobnhamtam = fntmstinfo.Ssobn;
                                        }
                                        ikk = ikk + 1;
                                        for (int j = 0; j < vieww.DataRowCount; j++)
                                        {
                                            ktranull = true;
                                            DataRow dr = vieww.GetDataRow(j);
                                            if (sobnhamtam == "")
                                                fntinfo.Sobn = fntmstinfo.Ssobn;
                                            else
                                            {
                                                fntmstinfo.Ssobn = sobnhamtam;
                                                fntinfo.Sobn = fntmstinfo.Ssobn;

                                            }
                                            fntinfo.Loaidvu = dr["loaidvu"].ToString();
                                            fntinfo.Dvucode = dr["dvucode"].ToString();
                                            fntinfo.SDonViTinh = ""; 
                                            fntinfo.Sdvt = "";
                                            fntinfo.Istt = j+1;
                                            fntinfo.Fslg = float.Parse(dr["soluong"].ToString());
                                            fntinfo.Ddonggia = decimal.Parse(dr["dongia"].ToString());
                                            fntinfo.Dthanhtien = decimal.Parse(dr["thanhtien"].ToString());
                                            tongDH = tongDH + fntinfo.Dthanhtien;
                                            fntinfo.Bnotrongoi =Uit.it_Parse.ToBoolean(dr["notrongoi"]);
                                            _sqlbf = _sqlbf + " " + fntctl.m_Insert_(fntinfo.Sobn, fntinfo.Loaidvu, fntinfo.Dvucode,
                                             fntinfo.SDonViTinh, fntinfo.Fslg, fntinfo.Dthanhtien, fntinfo.Ddonggia,fntinfo.Sdvt, fntinfo.Istt, fntinfo.Dgiabhyt, fntinfo.Bnotrongoi).ToString();
                                        }
                                        fntmstinfo.Sonhapvien = txtSoNhapVien_NL.Text;
                                        fntmstinfo.Dsotien = tongDH;
                                        addsobnDH = fntmstinfo.Ssobn;
                                        arrsbn.Add(fntmstinfo.Ssobn);
                                        if (ktranull == true)
                                            chuoibfmst =  fntmstctl.m_Insert_(fntmstinfo.Ssobn, fntmstinfo.Sdangay,
                                            fntmstinfo.Ssohs, fntmstinfo.Dsotien, fntmstinfo.SGhiChuUni, fntmstinfo.Iidbacsi, fntmstinfo.Smadvpk,
                                            fntmstinfo.SChanDoanUni, fntmstinfo.IIDLoaiPhieu, fntmstinfo.Sghichu, fntmstinfo.Schandoan, fntmstinfo.IIDNguoiNhap, fntmstinfo.Sonhapvien).ToString(); 
                                    }
                                    if (ktranull == true)
                                        sttdonvi = stt_;
                                }
                                else //khong thuoc madv=DH
                                {
                                    fntmstinfo.Ssobn = dtt.Rows[0]["sobn"].ToString();
                                   
                                    arrsbn.Add(fntmstinfo.Ssobn);
                                    stt_ = stt_ + 1;
                                    for (int j = 0; j < vieww.DataRowCount; j++)
                                    {
                                        ktranull = true;
                                        DataRow dr = vieww.GetDataRow(j);
                                        fntinfo.Sobn = fntmstinfo.Ssobn;
                                        fntinfo.Loaidvu = dr["loaidvu"].ToString();
                                        fntinfo.Dvucode = dr["dvucode"].ToString();
                                        fntinfo.SDonViTinh = ""; fntinfo.Sdvt = "";
                                        fntinfo.Istt = j+1;
                                        fntinfo.Fslg = float.Parse(dr["soluong"].ToString());
                                        fntinfo.Bnotrongoi =Uit.it_Parse.ToBoolean(dr["notrongoi"]);
                                        fntinfo.Ddonggia = decimal.Parse(dr["dongia"].ToString());
                                        fntinfo.Dthanhtien = decimal.Parse(dr["thanhtien"].ToString());
                                        tongdongia = tongdongia + fntinfo.Dthanhtien;
                                        _sqlbf = _sqlbf + " " + fntctl.m_Insert_(fntinfo.Sobn, fntinfo.Loaidvu, fntinfo.Dvucode,
                                               fntinfo.SDonViTinh, fntinfo.Fslg, fntinfo.Dthanhtien, fntinfo.Ddonggia,
                                               fntinfo.Sdvt, fntinfo.Istt, fntinfo.Dgiabhyt, fntinfo.Bnotrongoi).ToString();

                                    }
                                    fntmstinfo.Sonhapvien = txtSoNhapVien_NL.Text;
                                    fntmstinfo.Dsotien = tongdongia;
                                    if (ktranull == true)
                                        _sqlbfmst = _sqlbfmst + " " + fntmstctl.m_Insert_(fntmstinfo.Ssobn, fntmstinfo.Sdangay,
                                        fntmstinfo.Ssohs, fntmstinfo.Dsotien, fntmstinfo.SGhiChuUni, fntmstinfo.Iidbacsi, fntmstinfo.Smadvpk,
                                        fntmstinfo.SChanDoanUni, fntmstinfo.IIDLoaiPhieu, fntmstinfo.Sghichu, fntmstinfo.Schandoan, fntmstinfo.IIDNguoiNhap, fntmstinfo.Sonhapvien).ToString();
                                }
                                if (ktranull == true)
                                    sttdonvi = stt_;
                            }
                        }
                        _sqlbfmst = _sqlbfmst + " " + chuoibfmst;
                        arrsbn.Add(addsobnDH);
                    }
                    else if (i > 0)//thuoc.vtyt.dvyt.ptgp
                    {
                        bool flag = false, flag_ = false;//kiemtra paid :flag_ dang update
                        fntmstinfo.Ssobn =  fntmstctl.m_SoBN_KO_CLS_noitru(i + 1,madvpk_nhap).ToString();
                        for (int k = 0; k < view.DataRowCount; k++)
                        {
                            ktranull = false;
                            GridView vieww = (GridView)view.GetDetailView(k, 0);
                            if (vieww != null)//ptgp
                            {
                                DataRow drptgp = view.GetDataRow(k);
                                for (int j = 0; j < vieww.DataRowCount; j++)
                                {
                                    ktranull = true;
                                    DataRow dr = vieww.GetDataRow(j);
                                    fntinfo.Sobn = fntmstinfo.Ssobn;
                                    fntinfo.Loaidvu = dr["ma"].ToString();
                                    fntinfo.Dvucode = dr["dvucode"].ToString();
                                    fntinfo.SDonViTinh = "";
                                    fntinfo.Sdvt = "";
                                    fntinfo.Istt = j+1;
                                    fntinfo.Bnotrongoi =Uit.it_Parse.ToBoolean(dr["notrongoi"]);
                                    fntinfo.Fslg = float.Parse(dr["soluong"].ToString());
                                    fntinfo.Ddonggia = decimal.Parse(dr["dongia"].ToString());
                                    fntinfo.Dthanhtien = decimal.Parse(dr["thanhtien"].ToString());
                                    tongdongia = tongdongia + fntinfo.Dthanhtien;
                                    fntmstinfo.Dsotien = tongdongia;
                                    _sqlbf = _sqlbf + " " + fntctl.m_Insert_(fntinfo.Sobn, fntinfo.Loaidvu, fntinfo.Dvucode,
                                    fntinfo.SDonViTinh, fntinfo.Fslg, fntinfo.Dthanhtien, fntinfo.Ddonggia, fntinfo.Sdvt, fntinfo.Istt, fntinfo.Dgiabhyt,fntinfo.Bnotrongoi).ToString();
                                }
                            }
                            else if (i > 1)//thuoc.vtyt.dvyt
                            {
                                DataRow drq = view.GetDataRow(k);
                                fntinfo.Sobn = fntmstinfo.Ssobn;
                                if (drq["paid"].ToString() == "True")
                                    flag = true;
                                fntinfo.Loaidvu = "";
                                fntinfo.Dvucode = Uit.it_Parse.ToString(drq["dvucode"]);// xem lai
                                fntinfo.SDonViTinh = drq["dvt"].ToString();
                                fntinfo.Sdvt = Uit.it_Font.UnicodeToVNI(drq["dvt"].ToString());
                                fntinfo.Istt = k+1;
                                fntinfo.Dgiabhyt = Uit.it_Parse.ToDecimal(drq["giabhyt"], false);
                                fntinfo.Fslg = float.Parse(drq["soluong"].ToString());
                                fntinfo.Ddonggia = decimal.Parse(drq["dongia"].ToString());
                                fntinfo.Dthanhtien = decimal.Parse(drq["thanhtien"].ToString());
                                tongdongia = tongdongia + fntinfo.Dthanhtien;
                                fntmstinfo.Dsotien = tongdongia;
                                ktranull = true;
                                _sqlbf = _sqlbf + " " + fntctl.m_Insert_(fntinfo.Sobn, fntinfo.Loaidvu, fntinfo.Dvucode,
                                    fntinfo.SDonViTinh, fntinfo.Fslg, fntinfo.Dthanhtien, fntinfo.Ddonggia, fntinfo.Sdvt, fntinfo.Istt, fntinfo.Dgiabhyt, fntinfo.Bnotrongoi).ToString();
                            }
                        }

                        if (ktranull == true)
                        {
                            if (flag_ != true)
                                stt = stt + 1;
                            sttdetang = 1;
                            arrsbn.Add(fntmstinfo.Ssobn);
                            fntmstinfo.Sonhapvien = txtSoNhapVien_NL.Text;
                            _sqlbfmst = _sqlbfmst + " " + fntmstctl.m_Insert_( fntmstinfo.Ssobn, fntmstinfo.Sdangay,
                            fntmstinfo.Ssohs, fntmstinfo.Dsotien, fntmstinfo.SGhiChuUni, fntmstinfo.Iidbacsi, fntmstinfo.Smadvpk,
                            fntmstinfo.SChanDoanUni, fntmstinfo.IIDLoaiPhieu, fntmstinfo.Sghichu, fntmstinfo.Schandoan, fntmstinfo.IIDNguoiNhap,fntmstinfo.Sonhapvien).ToString();
                        }
                    }
                } 
            }
        }
        private void Updatedetailview(GridView view, int i, DataTable dt, int Istt)
        {
            decimal tongdongia = 0, tongtienguidi = 0; int idloaiphieu = 0;
            bool ktranull = false, ktraupdate = false; int lan = 0;
            string sobnDH = "",sogppt="";    string _chuoiupdate = ""; //nếu có update sobnDH thì tổng tiền sẽ không cộng tongDH_coRui
            if (view != null)
            {
                if (dt.Rows[0]["IDCaThuNgan"].ToString() != "")
                {
                    DataRow drvew = view.GetDataRow(i);
                    if (drvew == null) idloaiphieu = 0;
                    else
                        idloaiphieu = i + 1;
                    fntmstinfo.IIDLoaiPhieu = i + 1;
                    if (idloaiphieu == 1)//cls
                    {
                        object madvo; string chuoibfmst = ""; string ktra2lan = "", ktra2lan_="";//kiểm tra coico tao DH_01 rui chưa ?trường hợp thêm 2 số DH_ mà  trước đóa đã có DH_thuoc GA rùi(o day co 3 truong hop Dh_GA_dh,Ga_Dh_Dh)
                        decimal tongDH = 0; string addsobnDH = ""; int _stt = 0;//_sst>0 biết đã insert DH_ rùi  nếu thêm (GA thì DH_+1)
                        for (int h = 0; h < view.DataRowCount; h++)// kiểm tra có ma DH_ ko? có thì lưu lại SobnDH
                        {
                              DataRow drkt = view.GetDataRow(h);
                              string ssobn = drkt["sobn"].ToString();
                              if (ssobn!="")
                                  if (ssobn.Substring(0, 2) == "DH" && drkt["loaidvu"].ToString() != "GA")
                                      sobnDH = drkt["sobn"].ToString();
                        }
                        for (int k = 0; k < view.DataRowCount; k++)
                        {
                            ktranull = false;
                            GridView vieww = (GridView)view.GetDetailView(k,0);
                            if (vieww != null)
                            {
                                DataRow drmstcls = view.GetDataRow(k);
                                string loaidvu = drmstcls["loaidvu"].ToString();
                                madvo = dv_khac.DonViTuLoaidichVu(loaidvu);
                                DataTable dtt = fntmstctl.m_STT_SoBN_IDThuNgan_noitru(fntmstinfo.IIDNguoiNhap, fntmstinfo.Sdangay, madvo.ToString().Trim(), madvpk_nhap);
                                if (madvo.ToString().Trim() == "DH")
                                {
                                    if (loaidvu.Trim() == "GA")
                                    {
                                        if (_stt == 1 && sobnDH == "")
                                        {
                                            string[] s = fntmstinfo.Ssobn.Split('-');
                                            int td = Uit.it_Parse.ToInteger(s[1]) + 1;
                                            string tamduoi = Uit.it_String.Right("000" + td.ToString(), 4);
                                            fntmstinfo.Ssobn = s[0].ToString() + "-" + tamduoi;
                                        } 
                                        else
                                            fntmstinfo.Ssobn = dtt.Rows[0]["sobn"].ToString();
                                        for (int j = 0; j < vieww.DataRowCount; j++)
                                        {
                                            ktranull = true;
                                            DataRow dr = vieww.GetDataRow(j);
                                            if (drmstcls["sobn"].ToString() != "" )//xet truong hop update
                                            {
                                                if (dr["sobn"].ToString() != "")
                                                    ktraupdate = true;
                                               fntinfo.Sobn = drmstcls["sobn"].ToString();
                                               fntmstinfo.Ssobn = drmstcls["sobn"].ToString();
                                            }
                                            else
                                            {
                                                fntinfo.Sobn = fntmstinfo.Ssobn;
                                                _stt = 2;
                                            }
                                            fntinfo.Loaidvu = dr["loaidvu"].ToString();
                                            fntinfo.Dvucode = dr["dvucode"].ToString();
                                            fntinfo.SDonViTinh = ""; fntinfo.Sdvt = "";
                                            fntinfo.Istt = j + 1;
                                            fntinfo.Fslg = float.Parse(dr["soluong"].ToString());
                                            fntinfo.Bnotrongoi = Uit.it_Parse.ToBoolean(dr["notrongoi"]);
                                            fntinfo.Ddonggia = decimal.Parse(dr["dongia"].ToString());
                                            fntinfo.Dthanhtien = Uit.it_Parse.ToDecimal(fntinfo.Fslg, false) * Uit.it_Parse.ToDecimal(fntinfo.Ddonggia, false);
                                            tongtienguidi = tongtienguidi + fntinfo.Dthanhtien;
                                             _sqlbf = _sqlbf + " " + fntctl.m_Insert_(fntinfo.Sobn, fntinfo.Loaidvu, fntinfo.Dvucode,
                                            fntinfo.SDonViTinh, fntinfo.Fslg, fntinfo.Dthanhtien, fntinfo.Ddonggia, fntinfo.Sdvt, fntinfo.Istt, fntinfo.Dgiabhyt, fntinfo.Bnotrongoi).ToString();
                                        }
                                        fntmstinfo.Dsotien = tongtienguidi;
                                        arrsbn.Add(fntmstinfo.Ssobn);
                                        fntmstinfo.Sonhapvien = txtSoNhapVien_NL.Text;
                                        if (ktranull == true)
                                            _sqlbfmst = _sqlbfmst + " " + fntmstctl.m_Insert_(fntmstinfo.Ssobn, fntmstinfo.Sdangay,
                                          fntmstinfo.Ssohs, fntmstinfo.Dsotien, fntmstinfo.SGhiChuUni, fntmstinfo.Iidbacsi, fntmstinfo.Smadvpk,
                                          fntmstinfo.SChanDoanUni, fntmstinfo.IIDLoaiPhieu, fntmstinfo.Sghichu, fntmstinfo.Schandoan, fntmstinfo.IIDNguoiNhap, fntmstinfo.Sonhapvien).ToString();
                                        if (ktraupdate == true)// update 
                                           _sqlbfdelete = _sqlbfdelete + " " + fntmstctl.m_Delete_(fntmstinfo.Ssobn);
                                    }
                                    else //khong thuoc loai gui di "GA"(thuoc DH_)
                                    {
                                        ktraupdate = false;
                                        if (sobnDH == "")//insert mới chưa có chứng từ DH_
                                        {
                                            fntmstinfo.Ssobn = dtt.Rows[0]["sobn"].ToString();
                                            ktra2lan= fntmstinfo.Ssobn;
                                            lan = lan + 1;
                                        }
                                        else
                                            fntmstinfo.Ssobn = sobnDH;
                                        if (_stt == 2 && lan == 1)
                                        {
                                            string[] s = fntmstinfo.Ssobn.Split('-');
                                            int td = Uit.it_Parse.ToInteger(s[1]) + 1;
                                            string tamduoi = Uit.it_String.Right("000" + td.ToString(), 4);
                                            fntmstinfo.Ssobn = s[0].ToString() + "-" + tamduoi;
                                        }
                                        for (int j = 0; j < vieww.DataRowCount; j++)
                                        {
                                            ktranull = true;
                                            DataRow dr = vieww.GetDataRow(j);
                                            if (dr["sobn"].ToString() != "")//xet truong hop update
                                            {
                                                fntinfo.Sobn = dr["sobn"].ToString();
                                                ktraupdate = true;
                                            }
                                            else
                                            {
                                                _stt = 1;
                                                fntinfo.Sobn = fntmstinfo.Ssobn;
                                            }
                                            fntinfo.Loaidvu = dr["loaidvu"].ToString();
                                            fntinfo.Dvucode = dr["dvucode"].ToString();
                                            fntinfo.SDonViTinh = ""; fntinfo.Sdvt = ""; fntinfo.Istt = j+1;
                                            fntinfo.Fslg = float.Parse(dr["soluong"].ToString());
                                            fntinfo.Bnotrongoi = Uit.it_Parse.ToBoolean(dr["notrongoi"]);
                                            fntinfo.Ddonggia = decimal.Parse(dr["dongia"].ToString());
                                            fntinfo.Dthanhtien = Uit.it_Parse.ToDecimal(fntinfo.Fslg, false) * Uit.it_Parse.ToDecimal(fntinfo.Ddonggia, false);
                                            if (!ktraupdate)
                                            tongDH = tongDH + fntinfo.Dthanhtien;
                                            if(ktraupdate)
                                                _chuoiupdate = _chuoiupdate + " " + fntctl.m_Update_(fntinfo.Sobn, fntinfo.Loaidvu, fntinfo.Dvucode,
                                             fntinfo.SDonViTinh, fntinfo.Fslg, fntinfo.Dthanhtien, fntinfo.Ddonggia, fntinfo.Sdvt, fntinfo.Istt, fntinfo.Dgiabhyt, fntinfo.Bnotrongoi).ToString();
                                            else
                                            _sqlbf = _sqlbf + " " + fntctl.m_Insert_(fntinfo.Sobn, fntinfo.Loaidvu, fntinfo.Dvucode,
                                             fntinfo.SDonViTinh, fntinfo.Fslg, fntinfo.Dthanhtien, fntinfo.Ddonggia, fntinfo.Sdvt, fntinfo.Istt, fntinfo.Dgiabhyt, fntinfo.Bnotrongoi).ToString();
      
                                        }
                                        fntmstinfo.Dsotien = tongDH ;
                                        addsobnDH = fntmstinfo.Ssobn;
                                        fntmstinfo.Sonhapvien = txtSoNhapVien_NL.Text;
                                        if (ktranull == true)
                                        {
                                            if (sobnDH =="")
                                                chuoibfmst = fntmstctl.m_Insert_(fntmstinfo.Ssobn, fntmstinfo.Sdangay,
                                            fntmstinfo.Ssohs, fntmstinfo.Dsotien, fntmstinfo.SGhiChuUni, fntmstinfo.Iidbacsi, fntmstinfo.Smadvpk,
                                            fntmstinfo.SChanDoanUni, fntmstinfo.IIDLoaiPhieu, fntmstinfo.Sghichu, fntmstinfo.Schandoan, fntmstinfo.IIDNguoiNhap, fntmstinfo.Sonhapvien).ToString();
                                        }
                                    }
                                }
                                else //khong thuoc madv=DH
                                {
                                    ktraupdate = false;
                                    if (drmstcls["sobn"].ToString() != "")//xet truong hop update
                                    {
                                        fntmstinfo.Ssobn = drmstcls["sobn"].ToString();
                                        _sqlbfdelete = _sqlbfdelete + " " + fntmstctl.m_Delete_(fntmstinfo.Ssobn);
                                        ktraupdate = true;
                                    }
                                    else
                                        fntmstinfo.Ssobn = dtt.Rows[0]["sobn"].ToString();
                                    for (int j = 0; j < vieww.DataRowCount; j++)
                                    {
                                        ktranull = true;
                                        DataRow dr = vieww.GetDataRow(j);
                                        if (dr["sobn"].ToString() != "")//xet truong hop update
                                            fntinfo.Sobn = dr["sobn"].ToString();
                                        else
                                            fntinfo.Sobn = fntmstinfo.Ssobn;
                                        fntinfo.Loaidvu = dr["loaidvu"].ToString();
                                        fntinfo.Dvucode = dr["dvucode"].ToString();
                                        fntinfo.SDonViTinh = ""; fntinfo.Sdvt = "";
                                        fntinfo.Istt = j + 1;
                                        fntinfo.Fslg = float.Parse(dr["soluong"].ToString());
                                        fntinfo.Bnotrongoi = Uit.it_Parse.ToBoolean(dr["notrongoi"]);
                                        fntinfo.Ddonggia = decimal.Parse(dr["dongia"].ToString());
                                        fntinfo.Dthanhtien = Uit.it_Parse.ToDecimal(fntinfo.Fslg, false) * Uit.it_Parse.ToDecimal(fntinfo.Ddonggia, false);
                                        tongdongia = tongdongia + fntinfo.Dthanhtien;
                                        _sqlbf = _sqlbf + " " + fntctl.m_Insert_(fntinfo.Sobn, fntinfo.Loaidvu, fntinfo.Dvucode,
                                          fntinfo.SDonViTinh, fntinfo.Fslg, fntinfo.Dthanhtien, fntinfo.Ddonggia, fntinfo.Sdvt, fntinfo.Istt, fntinfo.Dgiabhyt, fntinfo.Bnotrongoi).ToString();

                                    }
                                    fntmstinfo.Sonhapvien = txtSoNhapVien_NL.Text;
                                    fntmstinfo.Dsotien = tongdongia;
                                    if (ktranull == true)
                                    {
                                        _sqlbfmst = _sqlbfmst + " " + fntmstctl.m_Insert_(fntmstinfo.Ssobn, fntmstinfo.Sdangay,
                                        fntmstinfo.Ssohs, fntmstinfo.Dsotien, fntmstinfo.SGhiChuUni, fntmstinfo.Iidbacsi, fntmstinfo.Smadvpk,
                                        fntmstinfo.SChanDoanUni, fntmstinfo.IIDLoaiPhieu, fntmstinfo.Sghichu, fntmstinfo.Schandoan, fntmstinfo.IIDNguoiNhap, fntmstinfo.Sonhapvien).ToString();
                                        arrsbn.Add(fntmstinfo.Ssobn);
                                    }
                                }
                              
                            }
                            else
                            {
                                DataRow drmstcls = view.GetDataRow(k);
                                string sobnadd = drmstcls["sobn"].ToString();
                                arrsbn.Add(sobnadd);
                            }
                        }
                        _sqlbfmst = _sqlbfmst + " " + chuoibfmst +" "+ _chuoiupdate;
                        if (addsobnDH !="")
                         arrsbn.Add(addsobnDH);
                    }
                    else if (i > 0)//thuoc.vtyt.dvyt.ptgp
                    {
                       fntmstinfo.Ssobn = fntmstctl.m_SoBN_KO_CLS_noitru(i + 1, madvpk_nhap).ToString();
                        if (i == 1)
                        {
                            for (int h = 0; h < view.DataRowCount; h++)// kiểm tra có ma DH_ ko? có thì lưu lại SobnDH
                            {
                                DataRow dr = view.GetDataRow(h);
                                if (dr["sobn"].ToString() != "")//xet truong hop update
                                {
                                    fntmstinfo.Ssobn = dr["sobn"].ToString();
                                    sogppt= dr["sobn"].ToString();
                                }
                            }
                        }
                        for (int k = 0; k < view.DataRowCount; k++)
                        { 
                            ktranull = false;
                            GridView vieww = (GridView)view.GetDetailView(k, 0);
                            if (vieww != null)//ptgp
                            {
                                ktraupdate = false;
                                for (int j = 0; j < vieww.DataRowCount; j++)
                                {
                                    ktranull = true;
                                    DataRow dr = vieww.GetDataRow(j);
                                    if (dr["sobn"].ToString() != "")//xet truong hop update 
                                       ktraupdate = true;
                                    fntinfo.Sobn = fntmstinfo.Ssobn;
                                    fntinfo.Loaidvu = dr["ma"].ToString();
                                    fntinfo.Dvucode = dr["dvucode"].ToString();
                                    fntinfo.SDonViTinh = "";
                                    fntinfo.Sdvt = "";
                                    fntinfo.Istt = j+1;
                                    fntinfo.Bnotrongoi =Uit.it_Parse.ToBoolean(dr["notrongoi"]);
                                    fntinfo.Fslg = float.Parse(dr["soluong"].ToString());
                                    fntinfo.Ddonggia = decimal.Parse(dr["dongia"].ToString());
                                    fntinfo.Dthanhtien = Uit.it_Parse.ToDecimal(fntinfo.Fslg, false) * Uit.it_Parse.ToDecimal(fntinfo.Ddonggia, false);
                                    if(!ktraupdate)
                                       tongdongia = tongdongia + fntinfo.Dthanhtien;
                                    if (ktraupdate)
                                        _chuoiupdate = _chuoiupdate + " " + fntctl.m_Update_(fntinfo.Sobn, fntinfo.Loaidvu, fntinfo.Dvucode,
                                     fntinfo.SDonViTinh, fntinfo.Fslg, fntinfo.Dthanhtien, fntinfo.Ddonggia, fntinfo.Sdvt, fntinfo.Istt, fntinfo.Dgiabhyt, fntinfo.Bnotrongoi).ToString();
                                    else
                                        _sqlbf = _sqlbf + " " + fntctl.m_Insert_(fntinfo.Sobn, fntinfo.Loaidvu, fntinfo.Dvucode,
                                         fntinfo.SDonViTinh, fntinfo.Fslg, fntinfo.Dthanhtien, fntinfo.Ddonggia, fntinfo.Sdvt, fntinfo.Istt, fntinfo.Dgiabhyt, fntinfo.Bnotrongoi).ToString();

                                }
                            }
                            else
                            {
                                DataRow drmstcls = view.GetDataRow(k);
                                string sobnadd = drmstcls["sobn"].ToString();
                                arrsbn.Add(sobnadd);
                            }
                            if (i > 1)//thuoc.vtyt.dvyt
                            {   
                                view.RefreshData();
                                DataRow drq = view.GetDataRow(k);
                                if (drq["sobn"].ToString() != "")//xet truong hop update
                                {
                                    fntmstinfo.Ssobn = drq["sobn"].ToString();
                                    if(k==0)
                                    _sqlbfdelete = _sqlbfdelete + " " + fntmstctl.m_Delete_(fntmstinfo.Ssobn);
                                    ktraupdate = true;
                                }
                                fntinfo.Sobn = fntmstinfo.Ssobn;
                                fntinfo.Loaidvu = "";
                                fntinfo.Dvucode = drq["dvucode"].ToString();
                                fntinfo.SDonViTinh = drq["dvt"].ToString();
                                fntinfo.Sdvt = Uit.it_Font.UnicodeToVNI(drq["dvt"].ToString());
                                fntinfo.Istt = k+1;
                                fntinfo.Dgiabhyt = Uit.it_Parse.ToDecimal(drq["giabhyt"], false);
                                fntinfo.Fslg = float.Parse(drq["soluong"].ToString());
                                fntinfo.Ddonggia = decimal.Parse(drq["dongia"].ToString());
                                fntinfo.Dthanhtien = Uit.it_Parse.ToDecimal(fntinfo.Fslg, false) * Uit.it_Parse.ToDecimal(fntinfo.Ddonggia, false);
                                fntinfo.Bnotrongoi = Uit.it_Parse.ToBoolean(drq["notrongoi"]);
                                tongdongia = tongdongia + fntinfo.Dthanhtien;
                                fntmstinfo.Dsotien = tongdongia;
                                ktranull = true;
                                if (ktraupdate)
                                    _sqlbf = _sqlbf + " " + fntctl.m_Update_(fntinfo.Sobn, fntinfo.Loaidvu, fntinfo.Dvucode,
                                fntinfo.SDonViTinh, fntinfo.Fslg, fntinfo.Dthanhtien, fntinfo.Ddonggia, fntinfo.Sdvt, fntinfo.Istt, fntinfo.Dgiabhyt, fntinfo.Bnotrongoi).ToString();
                                else
                                _sqlbf = _sqlbf + " " + fntctl.m_Insert_(fntinfo.Sobn, fntinfo.Loaidvu, fntinfo.Dvucode,
                                fntinfo.SDonViTinh, fntinfo.Fslg, fntinfo.Dthanhtien, fntinfo.Ddonggia, fntinfo.Sdvt, fntinfo.Istt, fntinfo.Dgiabhyt,fntinfo.Bnotrongoi).ToString();
                            }
                            else if (i==1)
                               sttdetang = 0;
                        }
                       if (ktranull == true)
                       {
                           _sqlbf = _sqlbf + " " + _chuoiupdate;
                            sttdetang = 1;
                            arrsbn.Add(fntmstinfo.Ssobn);
                            fntmstinfo.Sonhapvien = txtSoNhapVien_NL.Text;
                            if (ktraupdate != true)
                            {
                                if (i == 0)
                                {
                                    fntmstinfo.Dsotien = tongdongia;
                                    if (sobnDH == "")
                                        _sqlbfmst = _sqlbfmst + " " + fntmstctl.m_Insert_(fntmstinfo.Ssobn, fntmstinfo.Sdangay,
                                         fntmstinfo.Ssohs, fntmstinfo.Dsotien, fntmstinfo.SGhiChuUni, fntmstinfo.Iidbacsi, fntmstinfo.Smadvpk,
                                         fntmstinfo.SChanDoanUni, fntmstinfo.IIDLoaiPhieu, fntmstinfo.Sghichu, fntmstinfo.Schandoan, fntmstinfo.IIDNguoiNhap, fntmstinfo.Sonhapvien).ToString();
                                }
                                else
                                {
                                    if (i == 1 )
                                    {   if(sogppt=="")
                                        _sqlbfmst = _sqlbfmst + " " + fntmstctl.m_Insert_(fntmstinfo.Ssobn, fntmstinfo.Sdangay,
                                          fntmstinfo.Ssohs, fntmstinfo.Dsotien, fntmstinfo.SGhiChuUni, fntmstinfo.Iidbacsi, fntmstinfo.Smadvpk,
                                          fntmstinfo.SChanDoanUni, fntmstinfo.IIDLoaiPhieu, fntmstinfo.Sghichu, fntmstinfo.Schandoan, fntmstinfo.IIDNguoiNhap, fntmstinfo.Sonhapvien).ToString();
                                    }
                                    else 
                                        _sqlbfmst = _sqlbfmst + " " + fntmstctl.m_Insert_(fntmstinfo.Ssobn, fntmstinfo.Sdangay,
                                          fntmstinfo.Ssohs, fntmstinfo.Dsotien, fntmstinfo.SGhiChuUni, fntmstinfo.Iidbacsi, fntmstinfo.Smadvpk,
                                          fntmstinfo.SChanDoanUni, fntmstinfo.IIDLoaiPhieu, fntmstinfo.Sghichu, fntmstinfo.Schandoan, fntmstinfo.IIDNguoiNhap, fntmstinfo.Sonhapvien).ToString();
   


                                }
                            }

                       }
                   }
                }
            }

        }
        int sttdonvi_ = 0;//dung deluu STT bafeemst luc dau tien cua nut update
        private void btnLuu_Nhap_Click(object sender, EventArgs e)
        {      
            
            if (!madvpk_nhap.Equals("-1") && !cbosearchBacSi_Nhap.Text.Equals(""))
            {
                if (txtSoHS_Nhap.Text != "")
                {
                    arrsbn.Clear();
                    fntmstinfo.Sdangay = Uit.it_Date.CurrentDate("yyyy-MM-dd");
                    fntmstinfo.Ssohs = txtSoHS_Nhap.Text;
                    fntmstinfo.SGhiChuUni = txtGhiChu_Nhap.Text;
                    fntmstinfo.Sghichu = Uit.it_Font.UnicodeToVNI(txtGhiChu_Nhap.Text);
                    fntmstinfo.Iidbacsi = idbacsi_nhap;
                    fntmstinfo.Smadvpk = madvpk_nhap.Trim();
                    fntmstinfo.SChanDoanUni = txtChanDoan_Nhap.Text;
                    fntmstinfo.Schandoan = Uit.it_Font.UnicodeToVNI(txtChanDoan_Nhap.Text);
                    fntmstinfo.IIDNguoiNhap = frmLogin.nd.IDNguoiDung;
                    DataTable dt = fntmstctl.m_STT_SoBN_IDThuNgan_noitru(fntmstinfo.IIDNguoiNhap, fntmstinfo.Sdangay, madvpk_nhap.Trim(), madvpk_nhap);
                    GridView viewcls = (GridView)gridView_DMDV.GetDetailView(0, 0);
                    GridView viewpt = (GridView)gridView_DMDV.GetDetailView(1, 2);
                    GridView viewdvyt = (GridView)gridView_DMDV.GetDetailView(2, 4);
                    GridView viewvtyt = (GridView)gridView_DMDV.GetDetailView(3, 3);
                    //sttdonvi = Uit.it_Parse.ToInteger(dt.Rows[0]["stt"]);
                    //sttdonvi_ = Uit.it_Parse.ToInteger(dt.Rows[0]["stt"]);
                    tendonvithutien = dt.Rows[0]["tendonvi"].ToString();
                    if (viewcls != null)
                    {
                        Insertdetailview(viewcls, 0, dt, sttdonvi);
                    }
                    if (viewpt != null)
                    {
                        sttdonvi = sttdonvi + 1;
                        Insertdetailview(viewpt, 1, dt, sttdonvi);
                    }
                    if (viewdvyt != null)
                    {
                        sttdonvi = sttdonvi + 1;
                        Insertdetailview(viewdvyt, 2, dt, sttdonvi);
                        if (sttdetang == 0)
                            sttdonvi = sttdonvi - 1;
                    }
                    if (viewvtyt != null)
                    {
                        sttdonvi = sttdonvi + 1;
                        Insertdetailview(viewvtyt, 3, dt, sttdonvi);
                        if (sttdetang == 0)
                            sttdonvi = sttdonvi - 1;
                    }
                    string sqltong = _sqlbfmst + " " + _sqlbf;
                    if (Uit.it_SQL.ExecuteNonQuery(sqltong, true))
                    {
                        _sqlbf = "";
                        _sqlbfmst = "";
                        stt = 0;
                        sttdonvi = 0;
                        LoaiTapNhapSauInsert();
                        btnLuu_Nhap.Enabled = false;
                        // arrsbn.Clear();
                    }
                    else
                        XtraMessageBox.Show("Tạo Thất Bại", "Thông Báo");
                    _sqlbf = "";
                    _sqlbfmst = "";
                }
                else
                    XtraMessageBox.Show("Chưa chọn bệnh nhân", "Thông Báo");
            }
            else
                XtraMessageBox.Show("Chọn đơn vị","Thông Báo");
        }
        string xoatrung(ArrayList _resu)
        {
            string chuoi = "";
            for (int i = 0; i < _resu.Count - 1; i++)
            {
                for (int k = i + 1; k < _resu.Count; k++)
                {
                    if (_resu[i] == _resu[k])
                        _resu.RemoveAt(k);
                }
            }
            for (int i = 0; i < _resu.Count; i++)
            {
                if (i == _resu.Count - 1)
                {
                    string value = _resu[i] as string;
                    chuoi = chuoi + "''" + value + "''";
                }
                else
                {
                    string value = _resu[i] as string;
                    chuoi = chuoi + "''" + value + "'',";
                }
            }
            chuoi = "'(" + chuoi + ")'";
            return chuoi;
        }
        private void LoaiTapNhapSauInsert( )
        {
            DataSet dtset= new dsThuNgan();
            tongchiphi = 0;
            string chuoi = xoatrung(arrsbn);
            dtset = fntmstctl.m_LoadDSPhieuNhapTrucTiep_noitru(chuoi);
            ds.dt_CLS.Rows.Clear();
            for (int i = 0; i < dtset.Tables[1].Rows.Count; i++)
            {
                ds.Tables["dt_CLS"].ImportRow(dtset.Tables[1].Rows[i]);
            }
            ds.dt_Child_CTCLS.Rows.Clear();
            ds.InBienLai.Rows.Clear();
            ds.InBienLaiCLS.Rows.Clear();
            ds.ThongTinBL.Rows.Clear();
            for (int i = 0; i < dtset.Tables[2].Rows.Count; i++)
            {
                string ssbn = dtset.Tables[2].Rows[i]["sobn"].ToString();
                ssbn= ssbn.Substring(0, 2);
                if (ssbn == "DH")
                {
                    DataRow dr = ds.InBienLaiCLS.NewInBienLaiCLSRow();
                    dr["TenLoaiPhieu"] = dtset.Tables[2].Rows[i]["tenloai"];
                    dr["sobn"] = dtset.Tables[2].Rows[i]["sobn"];
                    dr["stt"] = dtset.Tables[2].Rows[i]["stt"];
                    dr["tendvu"] = dtset.Tables[2].Rows[i]["TenDichVU"];
                    dr["soluong"] = dtset.Tables[2].Rows[i]["soluong"];
                    dr["dongia"] = dtset.Tables[2].Rows[i]["dongia"];
                    dr["giabhyt"] = dtset.Tables[2].Rows[i]["giabhyt"];
                    dr["thanhtien"] = Uit.it_Parse.ToDouble(dr["dongia"]) * Uit.it_Parse.ToDouble(dr["soluong"]);
                    tongchiphi = tongchiphi + decimal.Parse(dr["thanhtien"].ToString());
                    ds.Tables["InBienLaiCLS"].Rows.Add(dr);
                }
                else
                {
                    DataRow dr = ds.InBienLai.NewInBienLaiRow();
                    dr["TenLoaiPhieu"] = dtset.Tables[2].Rows[i]["tenloai"];
                    dr["sobn"] = dtset.Tables[2].Rows[i]["sobn"];
                    dr["stt"] = dtset.Tables[2].Rows[i]["stt"];
                    dr["tendvu"] = dtset.Tables[2].Rows[i]["TenDichVU"];
                    dr["soluong"] = dtset.Tables[2].Rows[i]["soluong"];
                    dr["dongia"] = dtset.Tables[2].Rows[i]["dongia"];
                    dr["giabhyt"] = dtset.Tables[2].Rows[i]["giabhyt"];
                    dr["thanhtien"] = Uit.it_Parse.ToDouble(dr["dongia"])*Uit.it_Parse.ToDouble(dr["soluong"]);
                    tongchiphi = tongchiphi + decimal.Parse(dr["thanhtien"].ToString());
                    ds.Tables["InBienLai"].Rows.Add(dr);  
                }
                ds.Tables["dt_Child_CTCLS"].ImportRow(dtset.Tables[2].Rows[i]);
            }
            ds.dt_PTGP.Rows.Clear();//
            for (int i = 0; i <dtset.Tables[3].Rows.Count; i++)
            {
              ds.Tables["dt_PTGP"].ImportRow(dtset.Tables[3].Rows[i]);
            }
            ds.dt_CT_PTGP.Rows.Clear();
            for (int i = 0; i < dtset.Tables[4].Rows.Count; i++)
            {
                DataRow dr = ds.InBienLai.NewInBienLaiRow();
                dr["TenLoaiPhieu"] = dtset.Tables[4].Rows[i]["TenLoaiPT"];
                dr["sobn"] = dtset.Tables[4].Rows[i]["sobn"];
                dr["stt"] = dtset.Tables[4].Rows[i]["stt"];
                dr["tendvu"] = dtset.Tables[4].Rows[i]["tendvu"];
                dr["soluong"] = dtset.Tables[4].Rows[i]["soluong"];
                dr["dongia"] = dtset.Tables[4].Rows[i]["dongia"];
                dr["giabhyt"] = dtset.Tables[4].Rows[i]["giabhyt"];
                dr["thanhtien"] = Uit.it_Parse.ToDouble(dr["dongia"]) * Uit.it_Parse.ToDouble(dr["soluong"]);
                tongchiphi = tongchiphi + decimal.Parse(dr["thanhtien"].ToString());
                ds.Tables["InBienLai"].Rows.Add(dr);
                ds.Tables["dt_CT_PTGP"].ImportRow(dtset.Tables[4].Rows[i]);
            }
            ds.dt_DVYT.Rows.Clear();
            for (int i = 0; i < dtset.Tables[5].Rows.Count; i++)
            {
                DataRow dr = ds.InBienLai.NewInBienLaiRow();
                dr["TenLoaiPhieu"] = "Dịch Vụ Y Tế";
                dr["sobn"] = dtset.Tables[5].Rows[i]["sobn"];
                dr["stt"] = dtset.Tables[5].Rows[i]["stt"];
                dr["tendvu"] = dtset.Tables[5].Rows[i]["tendvu"];
                //dr["dvucode"] = dtset.Tables[5].Rows[i]["dvucode"];
                dr["dvt"] = dtset.Tables[5].Rows[i]["dvt"];
                dr["soluong"] = dtset.Tables[5].Rows[i]["soluong"];
                dr["dongia"] = dtset.Tables[5].Rows[i]["dongia"];
                dr["giabhyt"] = dtset.Tables[5].Rows[i]["giabhyt"];
                dr["thanhtien"] = Uit.it_Parse.ToDouble(dr["dongia"]) * Uit.it_Parse.ToDouble(dr["soluong"]);
                tongchiphi = tongchiphi + decimal.Parse(dr["thanhtien"].ToString());
                ds.Tables["InBienLai"].Rows.Add(dr);
                ds.Tables["dt_DVYT"].ImportRow(dtset.Tables[5].Rows[i]);
            }
            ds.dt_VTYT.Rows.Clear();
            for (int i = 0; i < dtset.Tables[6].Rows.Count; i++)
            {
                DataRow dr = ds.InBienLai.NewInBienLaiRow();
                dr["TenLoaiPhieu"] = "Vật Tư Y Tế";
                dr["sobn"] = dtset.Tables[6].Rows[i]["sobn"];
                dr["stt"] = dtset.Tables[6].Rows[i]["stt"];
                dr["tendvu"] = dtset.Tables[6].Rows[i]["tenvt"];
                dr["dvt"] = dtset.Tables[6].Rows[i]["dvt"];
                dr["soluong"] = dtset.Tables[6].Rows[i]["soluong"];
                dr["dongia"] = dtset.Tables[6].Rows[i]["dongia"];
                dr["giabhyt"] = dtset.Tables[6].Rows[i]["giabhyt"];
                dr["thanhtien"] = Uit.it_Parse.ToDouble(dr["dongia"]) * Uit.it_Parse.ToDouble(dr["soluong"]);
                tongchiphi = tongchiphi + decimal.Parse(dr["thanhtien"].ToString());
                ds.Tables["InBienLai"].Rows.Add(dr);
                ds.Tables["dt_VTYT"].ImportRow(dtset.Tables[6].Rows[i]);
            }
            ds.AcceptChanges();
            gridControl_NhapLieu.DataSource = ds;
            gridControl_NhapLieu.DataMember = "dt_dmloaiphieu";
        }
        private void btnIn_Nhap_Click(object sender, EventArgs e)
        {
            rpt_BienLaiThuTien bltt = new rpt_BienLaiThuTien();
            DataRow dribl = ds.ThongTinBL.NewThongTinBLRow();
            dribl["sohs"]= txtSoHoSo_DSPhieu.Text;
            dribl["donvithu"] = tendonvithutien;
            dribl["hoten"] = txtHoTen_DSPhieu.Text;
            dribl["nguoilap"]= "Người lập phiếu: "+frmLogin.nd.TenNguoiDung;
            dribl["nguoithu"]= "Người thu tiền: "+tenguoixacnhan;
            dribl["tongtien"]= tongchiphi;
            dribl["tuoi"] = txtTuoi_Nhap.Text;
            dribl["tieude"] = "Tổng cộng số tiền phải thu:";
            Clsdocso docs = new Clsdocso();
            dribl["stbangchu"] = " (" + docs.Doc(long.Parse(tongchiphi.ToString())) + ")";
            ds.ThongTinBL.Rows.Add(dribl);
            rpt_InBLTong rptth = new rpt_InBLTong(); 
            rptth.ShowPreview();
        }
        private void btnNhapMoi_Nhap_Click(object sender, EventArgs e)
        {
            ds = new dsThuNgan();
            ds.AcceptChanges();
            gridControl_NhapLieu.DataSource = ds;
            gridControl_NhapLieu.DataMember = "dt_dmloaiphieu";
        }
        private void cboseachDonVi_Nhap_EditValueChanged(object sender, EventArgs e)
        {
            madvpk_nhap = cboseachDonVi_Nhap.EditValue.ToString();
        }
        private void cbosearchBacSi_Nhap_EditValueChanged(object sender, EventArgs e)
        {
            idbacsi_nhap = Uit.it_Parse.ToInteger(cbosearchBacSi_Nhap.EditValue);
        }    
        private void btn_CapNhat_Nhap_Click(object sender, EventArgs e)
        {
            if (!madvpk_nhap.Equals("-1") && !cbosearchBacSi_Nhap.Text.Equals(""))
            {
                arrsbn.Clear();
                if (fntmstinfo.IIDNguoiNhap != frmLogin.nd.IDNguoiDung) return;
                fntmstinfo.Sdangay = Uit.it_Date.CurrentDate("yyyy-MM-dd");
                fntmstinfo.Ssohs = txtSoHS_Nhap.Text;
                fntmstinfo.SGhiChuUni = txtGhiChu_Nhap.Text;
                fntmstinfo.Sghichu = Uit.it_Font.UnicodeToVNI(txtGhiChu_Nhap.Text);
                fntmstinfo.Iidbacsi = idbacsi_nhap;
                fntmstinfo.Smadvpk = madvpk_nhap.Trim();
                fntmstinfo.SChanDoanUni = txtChanDoan_Nhap.Text;
                fntmstinfo.Schandoan = Uit.it_Font.UnicodeToVNI(txtChanDoan_Nhap.Text);
                fntmstinfo.IIDNguoiNhap = frmLogin.nd.IDNguoiDung;
                DataTable dt = fntmstctl.m_STT_SoBN_IDThuNgan_noitru(fntmstinfo.IIDNguoiNhap, fntmstinfo.Sdangay, madvpk_nhap.Trim(), madvpk_nhap);
                GridView viewcls = (GridView)gridView_DMDV.GetDetailView(0, 0);
                GridView viewpt = (GridView)gridView_DMDV.GetDetailView(1, 2);
                GridView viewdvyt = (GridView)gridView_DMDV.GetDetailView(2, 4);
                GridView viewvtyt = (GridView)gridView_DMDV.GetDetailView(3, 3);
              
                if (viewcls != null)
                {
                    Updatedetailview(viewcls, 0, dt, sttdonvi);
                }
                if (viewpt != null)
                {
                    sttdonvi = sttdonvi + 1;
                    Updatedetailview(viewpt, 1, dt, sttdonvi);
                    if (sttdetang == 0)
                        sttdonvi = sttdonvi - 1;
                }
                if (viewdvyt != null)
                {
                    sttdonvi = sttdonvi + 1;
                    Updatedetailview(viewdvyt, 2, dt, sttdonvi);
                    if (sttdetang == 0)
                        sttdonvi = sttdonvi - 1;
                }
                if (viewvtyt != null)
                {
                    sttdonvi = sttdonvi + 1;
                    Updatedetailview(viewvtyt, 3, dt, sttdonvi);
                    if (sttdetang == 0)
                        sttdonvi = sttdonvi - 1;
                }
                string _chuoi = "", _chuoisql = "";
                if (arrsbn.Count > 0)
                {
                    _chuoi = xoatrung(arrsbn);
                    _chuoisql = fntmstctl.m_Update_mst(_chuoi);
                }
               string sqltong = _sqlbfdelete + " " + _sqlbfmst + " " + _sqlbf + " " + _chuoisql;
               if (Uit.it_SQL.ExecuteNonQuery(sqltong, true))
               {
                    LoaiTapNhapSauInsert();
                    stt = 0;
                    sttdonvi = 0;
                    arrsbn.Clear();
                    sttdetang = 0;
                    sttdonvi_ = 0;
                    XtraMessageBox.Show("Cập nhật thành công", "Thông Báo");
                }
                 _sqlbf = "";
                 _sqlbfmst = ""; 
                 _sqlbfdelete = "";   
            }
            else
                XtraMessageBox.Show("Chọn đơn vị", "Thông Báo");
        }
        public  void ExpandAllRows(GridView view, int row,bool true_all)
        {
            view.BeginUpdate(); view.RefreshData();
            try
            {
                if (true_all)
                {
                    int dataRowCount = view.DataRowCount;
                    for (int rHandle = 0; rHandle < dataRowCount; rHandle++)
                    {
                        if (rHandle == row)
                            view.SetMasterRowExpanded(rHandle, true);
                        else
                            view.SetMasterRowExpanded(rHandle, false);
                    }
                }
                else
                    view.SetMasterRowExpanded(row, true);
            }
            finally
            {
                view.EndUpdate();
            }
        }
        #endregion
        private void OpenTap(string NameTap)
        {
            for (int i = 0; i < xtraTabControlChiPhiNGTru.TabPages.Count; i++)
            {
                if (xtraTabControlChiPhiNGTru.TabPages[i].Name.Equals(NameTap))
                {
                    xtraTabControlChiPhiNGTru.TabPages[i].PageEnabled = true;
                    // xtraTabControlChiPhiNGTru.TabPages[i].Focus();
                }
                else
                    xtraTabControlChiPhiNGTru.TabPages[i].PageEnabled = false;
            }
        }
        private void xtraTabControlChiPhiNGTru_SelectedPageChanged(object sender, DevExpress.XtraTab.TabPageChangedEventArgs e)
        {
                if (xtraTabControlChiPhiNGTru.SelectedTabPage == xtraTabPage_DSPhieu)
                {
                    Load_TapDanhSachPhieu();
                }
                if (xtraTabControlChiPhiNGTru.SelectedTabPage == xtraTabPage_Nhap)
                {
                    if (flag_TapPhieu)
                    {
                        btnLuu_Nhap.Enabled = false;
                        arrsbn.Clear();
                        arrsbn.Add(fntmstinfo.Ssobn);
                        LoaiTapNhapSauInsert();
                        txtCDCLS_Nhap.Text = fntmstinfo.SMaPhieuChiDinh;
                        txtChanDoan_Nhap.Text = fntmstinfo.SChanDoanUni;
                        txtGhiChu_Nhap.Text = fntmstinfo.SGhiChuUni;
                        cboseachDonVi_Nhap.EditValue =fntmstinfo.Smadvpk;
                        cbosearchBacSi_Nhap.EditValue = fntmstinfo.Iidbacsi;
                    }
                    else
                    {
                        btnLuu_Nhap.Enabled = true;
                        cboseachDonVi_Nhap.Focus();
                        cboseachDonVi_Nhap.EditValue = -1;
                        cbosearchBacSi_Nhap.EditValue = -1;
                        txtCDCLS_Nhap.Text = "";
                        txtGhiChu_Nhap.Text = "";
                        
                    }

                }
                if (xtraTabControlChiPhiNGTru.SelectedTabPage == xtraTabPage_DanhSachBN)
                {
                    
                    ds.ThongTinBL.Rows.Clear();
                    ds.dt_Child_CTCLS.Rows.Clear();
                    ds.InBienLai.Rows.Clear();
                    ds.InBienLaiCLS.Rows.Clear();
                    ds.dt_CLS.Rows.Clear();
                    ds.dt_CT_PTGP.Rows.Clear();
                    ds.dt_PTGP.Rows.Clear();
                    ds.dt_DVYT.Rows.Clear();
                    ds.dt_VTYT.Rows.Clear();
                    flag_TapPhieu = false;
                    ResetTapBenhNhan();
                }
                if (xtraTabControlChiPhiNGTru.SelectedTabPage == xtraTabPage_ChiDinh)
                {
                    // flag_TapPhieu = true;
                }
       
        }  
        private void gridView_DSPhieu_CustomDrawRowIndicator(object sender, RowIndicatorCustomDrawEventArgs e)
        {
            if (e.Info.IsRowIndicator)
            {
                if (e.RowHandle < 0)
                    e.Info.DisplayText = "";
                else
                    e.Info.DisplayText = (e.RowHandle + 1).ToString();
            }
            else
                e.Info.DisplayText = "STT";
            e.Info.Appearance.TextOptions.HAlignment = DevExpress.Utils.HorzAlignment.Far;
        }
        private void gridView_DSPhieu_DoubleClick(object sender, EventArgs e)
        {
               DataRow dr = gridView_DSPhieu.GetDataRow(gridView_DSPhieu.FocusedRowHandle);
                if (dr != null)
                {
                    flag_TapPhieu = true;
                    fntmstinfo.Ssobn = dr["sobn"].ToString();
                    fntmstinfo.IIDLoaiPhieu = int.Parse(dr["IDLoaiPhieu"].ToString());
                    fntmstinfo.Tenloaiphieu = dr["TenLoaiDichVu"].ToString();
                    fntmstinfo.Iidbacsi = int.Parse(dr["idbacsi"].ToString());
                    fntmstinfo.Smadvpk = dr["madvpk"].ToString();
                    //fntmstinfo.SMaPhieuChiDinh = dr["MaPhieuChiDinh"].ToString();
                    fntmstinfo.SGhiChuUni = dr["GhiChuUni"].ToString();
                    fntmstinfo.SChanDoanUni = dr["ChanDoanUni"].ToString();
                    fntmstinfo.IIDNguoiNhap = int.Parse(dr["IDNguoiNhap"].ToString());
                    OpenTap(xtraTabPage_Nhap.Name);
                    ExpandAllRows(gridView_DMDV, fntmstinfo.IIDLoaiPhieu-1, true);
                }
        }
        private void btnBNKhac_DSPhieu_Click(object sender, EventArgs e)
        {
            flag_TapPhieu = false;
            OpenTap(xtraTabPage_DanhSachBN.Name);
        }
        private void btnCapNhatPhieu_DSPhieu_Click(object sender, EventArgs e)
        {
            DataRow dr = gridView_DSPhieu.GetDataRow(gridView_DSPhieu.FocusedRowHandle);
            if (dr != null)
            {
                flag_TapPhieu = true;
                fntmstinfo.Ssobn = dr["sobn"].ToString();
                fntmstinfo.IIDLoaiPhieu = int.Parse(dr["IDLoaiPhieu"].ToString());
                fntmstinfo.Tenloaiphieu = dr["TenLoaiDichVu"].ToString();
                fntmstinfo.Iidbacsi = int.Parse(dr["idbacsi"].ToString());
                fntmstinfo.Smadvpk = dr["madvpk"].ToString();
                fntmstinfo.SMaPhieuChiDinh = dr["MaPhieuChiDinh"].ToString();
                fntmstinfo.SGhiChuUni = dr["GhiChuUni"].ToString();
                fntmstinfo.SChanDoanUni = dr["ChanDoanUni"].ToString();
                fntmstinfo.IIDNguoiNhap = int.Parse(dr["IDNguoiNhap"].ToString());
                OpenTap(xtraTabPage_Nhap.Name);
                ExpandAllRows(gridView_DMDV, fntmstinfo.IIDLoaiPhieu-1, true);
            }
        }
        private void gridControl_NhapLieu_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyData == Keys.Up)
            {
                //  detailView.Focus();
                if (detailView == null) return;
                int ss = detailView.FocusedRowHandle;
                if (ss == 0)
                {
                    gridView_DMDV.Focus();
                    gridView_DMDV.SetFocusedRowModified();
                }

            }
        }
        
        #endregion
        private void gridView_VTYT_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Tab)
            {
                GridControl grid = sender as GridControl;
                GridView view = gridView_Thuoc;
                if ((e.Modifiers == Keys.None && view.IsLastRow)

                    || (e.Modifiers == Keys.Shift && view.IsFirstRow && view.FocusedColumn.VisibleIndex == 0))
                {
                    if (view.IsEditing)
                        view.CloseEditor();
                    SelectNextControl(grid, e.Modifiers == Keys.None, false, false, true);
                    e.Handled = true;
                }

            }
        }

        private void simpleButton3_Click(object sender, EventArgs e)
        {
            Form f = FormHelpers.CheckChildFormShow("frm_RaToaThuoc", this);
            if (f == null)
            {
                frm_RaToaThuoc frm = new frm_RaToaThuoc();
                frm.MdiParent = this.ParentForm;
                frm.WindowState = FormWindowState.Maximized;
                frm.bninfo = bnhanInfo;
                frm.Show();
               
            }
            else
            {
                f.Activate();
                f.Focus();
            }
            
         //   this.Close();

        }

        private void btnTraThuoc_Click(object sender, EventArgs e)
        {
            Form f = FormHelpers.CheckChildFormShow("frm_TraLaiThuoc", this);
            if (f == null)
            {
                frm_TraLaiThuoc frm = new frm_TraLaiThuoc();
                frm.MdiParent = this.ParentForm;
                frm.WindowState = FormWindowState.Maximized;
                frm.bninfo = bnhanInfo;
                frm.Show();
            }
            else
            {
                f.Activate();
                f.Focus();
            }
        }   
    }
}